<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/1/25 13:29
 * FileName: Rule.php
 */

namespace app\common\model;


class Role extends Base
{
    protected $tip_name = '角色';
    protected $validate;
    protected $table;

    public function initialize()
    {
        parent::initialize();
        $this->validate=new \app\common\validate\Role();
        $this->table=config('database.prefix').'auth_group';
    }

    public function admin()
    {
        return $this->belongsToMany('Admin','authGroupAccess','uid','group_id');
    }

    //添加规则
    public function add($input){
        //验证重复
        $rule=$this->where('title',$input['title'])->find();
        if($rule){
            $this->res['msg']=$this->tip_name.'名已经存在！';
            return $this->res;
        }
        if(!isset($input['status']) || !$input['status']){
            $input['status']=0;
        }
        return $this->addModel($input);
    }

    //编辑规则
    public function edit($input){
        //验证重复
        $rule=$this->where('title',$input['title'])->where('id','NEQ',$input['id'])->find();
        if($rule){
            $this->res['msg']='规则名已经存在！';
            return $this->res;
        }
        if(!isset($input['status']) || !$input['status']){
            $input['status']=0;
        }
        return $this->editModel($input);
    }

    public function del($input){
        return $this->delModel($input);
    }
}